package edu.nf.project.webs.employeeAndCustomerInformation;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.nf.project.entitys.entity.other.dto.StaffInfoDTO;
import edu.nf.project.entitys.entity.employeeAndCustomerInformation.convention.StaffInfo;
import edu.nf.project.services.exception.derivedAnomalies.StaffInfoException;
import edu.nf.project.services.employeeAndCustomerInformation.StaffInfoService;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 员工管理控制器
 */
@RestController
@RequestMapping("/api/staff")
public class StaffInfoController extends BaseController {
    @Autowired
    private StaffInfoService staffInfoService;

    /**
     * 获取员工列表
     */
    @GetMapping
    public ResultVO<Page<StaffInfoDTO>> getStaffList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String staffName,
            @RequestParam(required = false) String phone) {
        try {
            Page<StaffInfoDTO> page = new Page<>(pageNum, pageSize);
            Page<StaffInfoDTO> result = staffInfoService.getStaffList(page, staffName, phone);
            return success(result);
        } catch (Exception e) {
            if (e instanceof StaffInfoException) {
                throw e;
            }
            System.out.println("错误：" + e);
            throw new StaffInfoException(20000, "系统错误，请稍后重试");
        }
    }

    /**
     * 获取员工详细信息
     */
    @GetMapping("/{staffId}")
    public ResultVO<StaffInfo> getStaffInfo(@PathVariable Integer staffId) {
        try {
            StaffInfo staffInfo = staffInfoService.getStaffInfo(staffId);
            if (staffInfo == null) {
                throw new StaffInfoException(20001, "员工不存在");
            }
            return success(staffInfo);
        } catch (Exception e) {
            if (e instanceof StaffInfoException) {
                throw e;
            }
            throw new StaffInfoException(20000, "系统错误，请稍后重试");
        }
    }

    /**
     * 更新员工信息
     */
    @PutMapping("/{staffId}")
    public ResultVO<Boolean> updateStaff(
            @PathVariable Integer staffId,
            @RequestBody StaffInfo staffInfo) {
        try {
            staffInfo.setStaffId(staffId);
            boolean result = staffInfoService.updateStaff(staffInfo);
            return success(result);
        } catch (Exception e) {
            if (e instanceof StaffInfoException) {
                throw e;
            }
            throw new StaffInfoException(20000, "系统错误，请稍后重试");
        }
    }

    /**
     * 根据用户ID创建新员工
     */
    @PostMapping("/assign-account")
    public ResultVO<Boolean> assignAccount(@RequestParam Integer uId) {
        try {
            boolean result = staffInfoService.assignAccount(uId);
            if (!result) {
                // 检查用户是否已经被分配为员工
                StaffInfo existingStaff = staffInfoService.getStaffInfo(uId);
                if (existingStaff != null) {
                    throw new StaffInfoException(20003, "该用户已经是员工");
                }
                throw new StaffInfoException(20002, "用户不存在");
            }
            return success(result);
        } catch (Exception e) {
            if (e instanceof StaffInfoException) {
                throw e;
            }
            throw new StaffInfoException(20000, "系统错误，请稍后重试");
        }
    }
}